package pearson.automation.forms.upload;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pearson.automation.forms.BrowseForm;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.ElementList;

public class UploadBranchForm extends BaseForm {
      
    private static Button uploadFiles = new Button(By.id("tab-upload-files"),  "Upload files");
    private static Button uploadVideo = new Button(By.id("tab-upload-videos"), "Upload videos");
    private static Button uploadLinks = new Button(By.id("tab-upload-links"),  "Upload links");
    private static Button close = new Button(By.linkText("Close"),  "Close");
    
    private static ElementList uploadedList = new ElementList(By.xpath("//fieldset/div[contains(@class, 'upload-item')]/div/h5"),  "Uploaded items");
    
    public UploadBranchForm() {
    super(uploadFiles.getLocator(), "Upload branch form");
    }
    
    public BrowseForm clickClose() {
        close.clickAndWait();
        return new BrowseForm();
    }

    public UploadFileForm clickUploadFile() {
        uploadFiles.clickAndWait();
        return new UploadFileForm();
    }
    
    public UploadVideoForm clickUploadVideo() {
        uploadVideo.clickAndWait();
        browser.getDriver().switchTo().frame(0);
        return new UploadVideoForm();
    }
    
    public UploadVideoLinkForm clickUploadLink() {
        uploadLinks.clickAndWait();
        return new UploadVideoLinkForm();
    }
    
    public Object[] getRecentlyAddedSection() {
        ArrayList<String> recentlyAdded = new ArrayList<String>();
        List<WebElement> list = uploadedList.getListElements();
        Collections.reverse(list);
        for (WebElement element: list) {
            recentlyAdded.add(element.getText());
        }
        return recentlyAdded.toArray();
    }
}
